package com.micolor.logr.demo.service.log.dao;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.micolor.logr.demo.entity.UserInfo;
import com.micolor.logr.demo.mapper.UserMapper;
import com.micolor.logr.runtime.annotation.LogRecord;
import com.micolor.logr.runtime.enums.OperateTypeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author EvanGe
 * @Type UserLogServiceImpl.java
 * @Desc
 * @date 2024/7/17 16:22
 */
@Service
@Slf4j
public class UserLogDao extends ServiceImpl<UserMapper, UserInfo> {
    @Autowired
    private UserMapper userMapper;

    @LogRecord(description = "添加用户",topic = "user_log_topic",entity = UserInfo.class,operate = OperateTypeEnum.INSERT)
    public UserInfo saveUserInfo(UserInfo userInfo) {
        userMapper.insert(userInfo);
        return userInfo;
    }

    @LogRecord(description = "更新用户",topic = "user_log_topic",entity = UserInfo.class,operate = OperateTypeEnum.UPDATE)
    public UserInfo updateUserInfo(UserInfo oldUserInfo ,UserInfo newUserInfo) {
        userMapper.updateById(newUserInfo);
        LambdaQueryWrapper<UserInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserInfo::getUserId,newUserInfo.getUserId());
        return this.getOne(queryWrapper);
    }


    @LogRecord(description = "删除用户",topic = "user_log_topic",entity = UserInfo.class,operate = OperateTypeEnum.DELETE)
    public UserInfo deleteUserInfo(UserInfo userInfo) {
        userMapper.deleteById(userInfo);
        return userInfo;
    }
}
